import { Routes, Route, Navigate, useNavigate } from "react-router-dom";
import "./App.scss";
import Login from "@/pages/login";
import Layout from "./common_components/layout";
import { routersData } from "./config";
import AdminManage from "./pages/adminManage";
import CorretExam from "./pages/corretExam";
import CorretExamList from "./pages/corretExamList";
import Exam from "./pages/exam";
import ExamHistory from "./pages/examHistory";
import ExamSelect from "./pages/examSelect";
import PersonInfo from "./pages/personInfo";
import ReadExam from "./pages/readExam";
import StudentManage from "./pages/studentManage";
import SubjectAdd from "./pages/subjectAdd";
import SubjectManage from "./pages/subjectManage";
import EventBus from "@/util/event";
import { notification } from "antd";
import { useEffect } from "react";
import { useDispatch } from "react-redux";
import { getUserInfo } from "./store/slice/userInfo";
import { useAppDispatch } from "./app/hooks";

function App() {
	const navigate = useNavigate();
	const dispatch = useAppDispatch();
	const [api, contextHolder] = notification.useNotification();

	const openNotification = (msg: string) => {
		api.error({
			message: "发生错误",
			description: `错误信息： ${msg}`,
		});
	};

	useEffect(() => {
		dispatch(getUserInfo());
		EventBus.on("global_not_login", function () {
			navigate("/login");
		});

		EventBus.on("global_error_tips", function (msg: string) {
			openNotification(msg);
		});

		EventBus.on("global_error_auth", function (msg: string) {
			openNotification(msg);
		});
	}, []);

	return (
		<>
			{contextHolder}
			<Routes>
				<Route element={<Layout />}>
					<Route path="/" element={<Navigate to="/login"></Navigate>} />
					{/*页面12： 管理员管理 （超级管理员） */}
					<Route
						path={routersData.admin_manage.path}
						element={<AdminManage />}
					></Route>
					{/* 页面8： 批改试卷（管理员） */}
					<Route
						path={routersData.corret_exam.path}
						element={<CorretExam />}
					></Route>
					{/* 页面7： 批阅试卷列表（管理员） */}
					<Route
						path={routersData.corret_exam_list.path}
						element={<CorretExamList />}
					></Route>
					{/* 页面4： 考试 (学生) */}
					<Route path={routersData.exam.path} element={<Exam />}></Route>
					{/* 页面5： 学生考试记录 （学生） */}
					<Route
						path={routersData.exam_history.path}
						element={<ExamHistory />}
					></Route>
					{/* 页面3： 考题选择 （学生） */}
					<Route
						path={routersData.exam_select.path}
						element={<ExamSelect />}
					></Route>
					{/* 页面2： 个人信息录入（学生 管理员） */}
					<Route
						path={routersData.person_info.path}
						element={<PersonInfo />}
					></Route>
					{/* 页面6 查看试卷（学生 管理员） */}
					<Route
						path={routersData.read_exam.path}
						element={<ReadExam />}
					></Route>
					{/* 页面9： 学生管理(管理员) */}
					<Route
						path={routersData.student_manage.path}
						element={<StudentManage />}
					></Route>
					{/* 页面11： 考题录入（管理员） */}
					<Route
						path={routersData.subject_add.path}
						element={<SubjectAdd />}
					></Route>
					{/* 页面10： 课程管理 （管理员） */}
					<Route
						path={routersData.subject_manage.path}
						element={<SubjectManage />}
					></Route>
				</Route>
				<Route path={routersData.login.path} element={<Login />} />
			</Routes>
		</>
	);
}

export default App;
